package com.weedsdream.app.colorplate

import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableFloatStateOf
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
import com.weedsdream.app.colorplate.Axis3.Type.B
import com.weedsdream.app.colorplate.Axis3.Type.G
import com.weedsdream.app.colorplate.Axis3.Type.R
import com.weedsdream.app.colorplate.Axis3.following
import com.weedsdream.app.colorplate.Axis3.pos3
import com.weedsdream.app.colorplate.Axis3.radius
import com.weedsdream.app.colorplate.Axis3.rgb
import com.weedsdream.app.colorplate.Axis3.rgbChange
import com.weedsdream.app.colorplate.Axis3.type


/**第三轴参数
 * @property radius 第三参数轴判定半径
 * @property rgb 第三轴参数值:0-255
 * @property rgbChange 每当rgb值变化时+1，强制触发重绘
 * @property type 第三轴参数类型
 * @property following 当前第三轴是否在跟随
 * @property pos3 第三轴点位置*/
internal object Axis3 {
    var radius by mutableFloatStateOf(0f)
    var rgbChange by mutableIntStateOf(0)
    var rgb by mutableIntStateOf(0)
    var type by mutableStateOf<Type?>(null)
    var following = false
    var pos3 by mutableStateOf(Offset.Zero)

    /**类型*/
    enum class Type {
        R, G, B
    }

    /**计算第三坐标轴数值
     *
     * type:
     * - 如果r、g、b存在0值，一定被设置为null.否则如果type==null时根据r、g、b最大值指定type,否则type保持或因为当前type对应的r、g、b小于指定值被设置为null
     *
     * rgb:
     * - 仅type!=null时有效，只在0-255之间，代表第三轴颜色值。当type有效时，取type对应的r、g、b值
     *
     * 色盘：
     * - 当type或rgb有变化时，修改色盘显示内容
     *
     * pos3:
     * - 只根据type和rgb值计算位置*/
    fun computeAxis3(r: Int, g: Int, b: Int) {
        val oldT = type
        val oldV = rgb
        //计算type和rgb
        if (r == 0 || g == 0 || b == 0) type = null else {
            if (type == null) {
                rgb = maxOf(r, g, b)
                type = when (rgb) {
                    r -> R
                    g -> G
                    b -> B
                    else -> throw Exception("不该执行")
                }
            } else when (type!!) {
                R -> if (r < 10) type = null else rgb = r
                G -> if (g < 10) type = null else rgb = g
                B -> if (b < 10) type = null else rgb = b
            }
        }
        if (oldT == type && oldV == rgb) return //无变化
        //计算轴点位置
        pos3 = if (type == null) Offset.Zero else when (type!!) {
            R -> color2offset(Color(rgb, 0, 0))
            G -> color2offset(Color(0, rgb, 0))
            B -> color2offset(Color(0, 0, rgb))
        }
        //计算受轴点影响的扇面显示色
        if (oldT != type && oldT != null) when (oldT) {//恢复原扇面
            R -> colorPointsR.forEach { it.show = Color(0, it.g, it.b) }
            G -> colorPointsG.forEach { it.show = Color(it.r, 0, it.b) }
            B -> colorPointsB.forEach { it.show = Color(it.r, it.g, 0) }
        }
        if (type != null) when (type!!) {//修改新扇面
            R -> colorPointsR.forEach { it.show = Color(rgb, it.g, it.b) }
            G -> colorPointsG.forEach { it.show = Color(it.r, rgb, it.b) }
            B -> colorPointsB.forEach { it.show = Color(it.r, it.g, rgb) }
        }
        rgbChange++
    }
}